import { _decorator, Component, Node, resources, Sprite, SpriteFrame } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('DiceController')
export class DiceController extends Component {
    private num: number;
    private used: boolean;

    start() {
    }

    update(deltaTime: number) {
    }

    public throwDice(): number {
        this.num = this.getRandomNum(1, 6);
        this.used = false;

        resources.load("images/dice/Dice-" + this.num + "/spriteFrame", SpriteFrame, (err, spriteFrame) => {
            this.node.getComponent(Sprite).spriteFrame = spriteFrame;
        });

        return this.num;
    }

    private getRandomNum(min, max): number {
        var range = max - min;
        var rand = Math.random();
        return (min + Math.round(rand * range));
    }

    public setUsed(value: boolean): void {
        this.used = value;
    }

    public IsUsed(): boolean {
        return this.used;
    }

    public getNum(): number {
        return this.num;
    }
}


